package hongcaosp.app.android.search;

import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.RGFragmentPagerAdapter;
import xlj.lib.android.base.component.BaseActivity;
import xlj.lib.android.base.component.BaseFragment;
import xlj.lib.android.base.utils.StatusBarUtil;
import xlj.lib.android.base.view.ClearEditTextView;
import xlj.lib.android.base.view.SwitchWidget;

public class SearchActivity extends BaseActivity {
    private ClearEditTextView searchView;
    private ViewPager viewPager;
    private SwitchWidget tabSwitch;
    private TextView tabOne,tabTwo;
    private List<BaseFragment> fragments = new ArrayList<>();

    private List<SearchEventListener> searchEventListeners = new ArrayList<>();
    @Override
    protected void initData(Intent intent) {

    }

    @Override
    protected void initView() {
        StatusBarUtil.setStatusBarDarkTheme(this,false);

        findViewById(R.id.back_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        searchView = findViewById(R.id.search_bar);

        viewPager = findViewById(R.id.view_pager);
        tabSwitch = findViewById(R.id.switch_tabs);
        tabOne = tabSwitch.findViewById(R.id.text_one);
        tabTwo = tabSwitch.findViewById(R.id.text_two);
        tabSwitch.setOnSwitchedListener(new SwitchWidget.OnSwitchedListener() {
            @Override
            public boolean onItemSelected(int index) {
                viewPager.setCurrentItem(index,true);
                return true;
            }
        });
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabSwitch.setItemSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        fragments.clear();
        SearchVideoFragment searchVideoFragment = new SearchVideoFragment();
        searchEventListeners.add(searchVideoFragment);
        fragments.add(searchVideoFragment);
        SearchUserFragment searchUserFragment = new SearchUserFragment();
        fragments.add(searchUserFragment);
        searchEventListeners.add(searchUserFragment);

        RGFragmentPagerAdapter RGFragmentPagerAdapter = new RGFragmentPagerAdapter(getSupportFragmentManager(), fragments, new String[]{"视频", "用户"});
        viewPager.setAdapter(RGFragmentPagerAdapter);

        findViewById(R.id.search_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (SearchEventListener searchEventListener : searchEventListeners) {
                    searchEventListener.onStartSearch(searchView.getText().toString());
                }
            }
        });

    }

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_search;
    }
}
